//
//  LSCompetitionSubViewController.m
//  LookScore
//
//  Created by wws on 2018/4/12.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSCompetitionSubViewController.h"
#import "LSCompetitionModel.h"
#import "LSCompetitionActionCell.h"
#import "LSLiveCompetitionViewController.h"
#import "LSCustomHeaderView.h"
#import "LSTimeSelectView.h"
#import "LSMatchScheduleModel.h"
#import "FSSegmentTitleView.h"
#import "LSDataLeagueModel.h"
#import "LSComSecionHeaderView.h"
#import "LSTimerManeger.h"
//#import <UITableView+FDTemplateLayoutCell.h>
//#import "YYFPSLabel.h"
#import "LSLayout.h"
#import <YYKit.h>
@interface LSCompetitionSubViewController ()<UITableViewDelegate,UITableViewDataSource,FSSegmentTitleViewDelegate>
@property (strong, nonatomic)UITableView *tableView;

@property (nonatomic, strong) NSMutableArray * modelCopyArray;//即时筛选数组

@property (nonatomic, assign) NSInteger type;//类型--关注，即时，完场，赛程。

@property (nonatomic, strong) NSMutableArray * timeArray;

@property (nonatomic, copy) NSString * timeStr;

@property (nonatomic, copy) NSString * headerLabelStr;

@property (nonatomic, strong) NSMutableArray * matchModelArray;

@property (nonatomic, strong) NSMutableArray * wancCopyArray;//完场刷选数组

@property (nonatomic, strong) NSMutableArray * scheduleArray;



@property (nonatomic, strong) NSMutableArray *wancLeagueArray;
@property (nonatomic, strong) NSMutableArray *leagueArray;
@property (nonatomic, strong) NSMutableArray * zouArray;
@property (nonatomic, assign) NSInteger scheduleTitleViewSelected;
@property (nonatomic, assign) BOOL isInitVC;


@property (nonatomic, strong) NSMutableArray * MatchRunningListArray;
@property (nonatomic, strong) NSMutableArray * MatchWaitListArray;
@property (nonatomic, strong) NSMutableArray * MatchEndListArray;
@property (nonatomic, strong) NSMutableArray * MatchDelayListArray;
@property (nonatomic, strong) NSMutableArray * MatchCancelListArray;
@property (nonatomic, strong) NSMutableArray * MatchUnsureListArray;
@property (nonatomic, strong) NSMutableArray * sectionArray;
@property (nonatomic, strong) NSTimer * timer;
//@property (nonatomic, strong) YYFPSLabel *fpsLabel;
@end

@implementation LSCompetitionSubViewController
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (NSMutableArray *)MatchRunningListArray{
    if (!_MatchRunningListArray) {
        _MatchRunningListArray = @[].mutableCopy;
    }
    return _MatchRunningListArray;
}
- (NSMutableArray *)MatchWaitListArray{
    if (!_MatchWaitListArray) {
        _MatchWaitListArray = @[].mutableCopy;
    }
    return _MatchWaitListArray;
}

- (NSMutableArray *)MatchEndListArray{
    
    if (!_MatchEndListArray) {
        _MatchEndListArray = @[].mutableCopy;
    }
    return _MatchEndListArray;
}

- (NSMutableArray *)MatchDelayListArray{
    if (!_MatchDelayListArray) {
        _MatchDelayListArray = @[].mutableCopy;
    }
    return _MatchDelayListArray;
}

- (NSMutableArray *)MatchCancelListArray{
    
    if (!_MatchCancelListArray) {
        _MatchCancelListArray = @[].mutableCopy;
    }
    return _MatchCancelListArray;
}

- (NSMutableArray *)MatchUnsureListArray{
    
    if (!_MatchUnsureListArray) {
        _MatchUnsureListArray = @[].mutableCopy;
    }
    return _MatchUnsureListArray;
}

- (NSMutableArray *)modelArray{
    if(!_modelArray){
        _modelArray=[[NSMutableArray alloc]init];
    }
    return _modelArray;
}

- (NSMutableArray *)wancLeagueArray{
    if (!_wancLeagueArray) {
        _wancLeagueArray = @[].mutableCopy;
    }
    
    return _wancLeagueArray;
}

- (NSMutableArray *)leagueArray{
    if (!_leagueArray) {
        _leagueArray =@[].mutableCopy;
    }
    return _leagueArray;
}
- (NSMutableArray *)zouArray{
    if (!_zouArray) {
        _zouArray = @[].mutableCopy;
    }
    return _zouArray;
}
- (NSMutableArray *)modelCopyArray{
    if (!_modelCopyArray) {
        _modelCopyArray = @[].mutableCopy;
    }
    return _modelCopyArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    LSRefreshHeaderView *header = [LSRefreshHeaderView headerWithRefreshingBlock:^{
        
        switch (self.type) {
            case 0:{
                [self getSaveEvent];
            }
                break;
            case 1:{
                
                [self loadData:YES withUrlStr:DATABASEMATCHPAGE WithType:self.type with:@""];
            }
                break;
            case 2:{
                [self loadData:YES withUrlStr:DATABASENDMACTH WithType:self.type with:_timeStr];
            }
                break;
            case 3:{
                
                [self loadData:YES withUrlStr:DATABASEMATCHSCHEDULE WithType:self.type with:_timeStr];
            }
                break;
            default:
                break;
        }
    }];
    self.tableView.mj_header = header;// 设置header
    [header beginRefreshing];
}

- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    _headerLabelStr = [NSString stringWithFormat:@"%@ %@",[CommonClass readCurrentTimeString],[CommonClass readWeekDay]];
}

- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    
    switch (index) {
        case 0:{
            
            [self getSaveEvent];
        }
            break;
        case 1:{
            //            _headerLabelStr = [NSString stringWithFormat:@"%@ %@",[CommonClass readCurrentTimeString],[CommonClass readWeekDay]];
            [kLSTimerManeger start];
            [self startTimer];
        }
            break;
        case 2:{
            self.timeArray = [CommonClass readBeforeSevenTimeStringCurrenTimeWithType:0].mutableCopy;
            self.scheduleArray = [CommonClass readBeforeSevenTimeStringCurrenTimeWithType:1].mutableCopy;
            
        }
            break;
        case 3:{
            self.timeArray = [CommonClass readAfterSevenTimeStringWithCurrentTimeWithzType:0].mutableCopy;
            self.scheduleArray = [CommonClass readAfterSevenTimeStringWithCurrentTimeWithzType:1].mutableCopy;
        }
            break;
        default:
            break;
    }
    if (_isInitVC) {
        
        if (self.type == 1) {
            
            //            NSMutableArray * array = @[].mutableCopy;
            NSMutableArray * array;
            if (self.optionType == 1) {
                array = [self judgeScreeningButtonIsSelectedWithModelArray:self.leagueArray WithNameArray:self.leagueNameArray];
                //                self.block(leagueArray,leagueArray,self.modelCopyArray.mutableCopy,self.type);
            }else if (self.optionType == 2){
                array = [self judgeScreeningButtonIsSelectedWithModelArray:self.leagueArray WithNameArray:self.zouLeagueNamaArray];
                //                self.block(zouLeagueArray,zouLeagueArray,self.modelCopyArray.mutableCopy,self.type);
            }else{
                array = self.leagueArray;
            }
            self.block(array,array,self.modelCopyArray.mutableCopy,self.type);
        }else if (self.type == 2){
            
            NSMutableArray * wancLeagueArray = [self judgeScreeningButtonIsSelectedWithModelArray:self.wancLeagueArray WithNameArray:self.leagueNameArray];
            NSMutableArray * zouWancLeagueArray = [self judgeScreeningButtonIsSelectedWithModelArray:self.wancLeagueArray WithNameArray:self.leagueNameArray];
            self.block(wancLeagueArray,zouWancLeagueArray,self.wancCopyArray.mutableCopy,self.type);
        }else{
            self.block(@[].mutableCopy,@[].mutableCopy,@[].mutableCopy, self.type);
        }
    }
}
/**判断筛选控制器筛选按钮是否选中*/
- (NSMutableArray *)judgeScreeningButtonIsSelectedWithModelArray:(NSMutableArray *)modelArray WithNameArray:(NSMutableArray *)nameArray{
    
    NSMutableArray * array = modelArray;
    for (LSDataLeagueModel * model in array) {
        if (nameArray.count != 0) {
            BOOL isHas = YES;
            for (NSString * name  in nameArray) {
                if ([name isEqualToString:model.LeagueName_CN]) {
                    isHas = NO;
                }
            }
            if (isHas) {
                model.isBtnSelected = NO;
            }else{
                model.isBtnSelected = YES;
            }
        }else{
            if (model.IsTopClassLeague) {
                model.isBtnSelected = YES;
            }else{
                model.isBtnSelected = NO;
            }
        }
    }
    return array;
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    if (index == 1) {
        //结束定时器
        [_timer invalidate];
    }
}

- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    self.type = index;
    _isInitVC = YES;
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftEqualToView(self.view)
    .topSpaceToView(self.view, 0)
    .rightEqualToView(self.view)
    .heightIs(LSScreenHeight-150);
    
    
    switch (self.type) {
        case 0:{
            
            //            [self getSaveEvent];
        }
            
            break;
        case 1:{
            _timeStr = [CommonClass readCurrentTimeString];
            //            [self loadData:YES withUrlStr:DATABASEMATCHPAGE WithType:self.type with:@""];
//            _fpsLabel = [YYFPSLabel new];
//            [_fpsLabel sizeToFit];
//            _fpsLabel.bottom = self.view.height - 300;
//            _fpsLabel.left = 12;
//            _fpsLabel.alpha = 1;
//            [self.view addSubview:_fpsLabel];
        }
            break;
        case 2:{
            self.scheduleArray = [CommonClass readBeforeSevenTimeStringCurrenTimeWithType:1].mutableCopy;
            _timeStr = self.scheduleArray.firstObject;
            //            [self loadData:YES withUrlStr:DATABASENDMACTH WithType:self.type with:_timeStr];
        }
            break;
        case 3:{
            self.scheduleArray = [CommonClass readAfterSevenTimeStringWithCurrentTimeWithzType:1].mutableCopy;
            _timeStr = self.scheduleArray.firstObject;
            //            [self loadData:YES withUrlStr:DATABASEMATCHSCHEDULE WithType:self.type with:_timeStr];
            
        }
            break;
        default:
            break;
    }
    
}

- (void)updateData{
    [self loadData:YES withUrlStr:DATABASEMATCHPAGE WithType:self.type with:@""];
}
#pragma mark -- 加载数据源
- (void)loadData:(BOOL)refresh withUrlStr:(NSString *)urlStr WithType:(NSInteger)type with:(NSString *)dateStr{
    NSDictionary * dict;
    if (type >= 2) {
        dict = @{@"date":dateStr};
    }else{
        dict = @{};
    }
    [LSHttpEnclosureTool GET:urlStr parameters:dict resBlock:^(id responseObject) {
        if (responseObject) {
            if (refresh) {
                [self.modelArray removeAllObjects];
                [self.leagueArray removeAllObjects];
                [self.wancLeagueArray removeAllObjects];
            }
            if (self.type == 1) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.MatchCancelListArray removeAllObjects];
                    [self.MatchDelayListArray removeAllObjects];
                    [self.MatchEndListArray removeAllObjects];
                    [self.MatchWaitListArray removeAllObjects];
                    [self.MatchRunningListArray removeAllObjects];
                    [self.MatchUnsureListArray removeAllObjects];
                    [self throughTheArray:responseObject];
                });
                
            }else if (self.type == 3){
                for (NSDictionary * dict in responseObject) {
                    LSMatchScheduleModel * model = [LSMatchScheduleModel mj_objectWithKeyValues:dict];
                    [self.modelArray addObject:model];
                }
                [self.tableView reloadData];
                [CommonClass endRefreshing:self.tableView];
            }else if (self.type == 2){
                for (NSDictionary * dict in responseObject[@"EndMatchList"]) {
                    LSCompetitionModel * model = [LSCompetitionModel mj_objectWithKeyValues:dict];
                    LSLayout * layout = [[LSLayout alloc] initWith:model];
                    [self.modelArray addObject:layout];
                }
                self.wancCopyArray = self.modelArray.mutableCopy;
                for (NSDictionary *dict in responseObject[@"DataLeagueList"]) {
                    LSDataLeagueModel * model = [LSDataLeagueModel mj_objectWithKeyValues:dict];
                    if (model.IsTopClassLeague) {
                        model.isBtnSelected = YES;
                    }else{
                        model.isBtnSelected = NO;
                    }
                    [self.wancLeagueArray addObject:model];
                    self.zouArray = self.wancLeagueArray.mutableCopy;
                }
                self.block(self.wancLeagueArray.mutableCopy,self.zouArray ,self.wancCopyArray,self.type);
                [self.tableView reloadData];
                [CommonClass endRefreshing:self.tableView];
            }
            
        }else{
            
            [CommonClass endRefreshing:self.tableView];
            [self.tableView reloadData];
        }
        
    }];
    
}
#pragma mark -- 循环加载数据到每个数组
- (void)throughTheArray:(id)response{
    MyLog(@"开始");
    
    DISPATCH_ASYNC_GET_GLOBAL(^{
        DISPATCH_ASYNC_GET_MAIN((^{
            
            [[response[@"MatchRunningList"] mutableCopy] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary * dict = obj;
                LSCompetitionModel * model = [LSCompetitionModel mj_objectWithKeyValues:dict];
                [[LSAccountArchive getSaveEventArray] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString * macId = obj;
                    if (macId.integerValue == model.MatchId) {
                        model.isSelected = YES;
                        LSCompetitionModel * cacelModel = [LSCompetitionModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE MatchId=%ld",(long)model.MatchId]];
                        if (cacelModel) {
                            model.pk = cacelModel.pk;
                            model.currentTime = cacelModel.currentTime;
                            [model update];
                        }
                    }
                }];
                LSLayout * layout = [[LSLayout alloc] initWith:model];
                [self.MatchRunningListArray addObject:layout];
            }];
            
        }));
        
    });
    
    DISPATCH_ASYNC_GET_GLOBAL(^{
        DISPATCH_ASYNC_GET_MAIN((^{
            MyLog(@"startWait = %ld", [response[@"MatchWaitList"] count]);
            [[response[@"MatchWaitList"] mutableCopy] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary * dict = obj;
                LSCompetitionModel * model = [LSCompetitionModel mj_objectWithKeyValues:dict];
                [[LSAccountArchive getSaveEventArray] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString * macId = obj;
                    if (macId.integerValue == model.MatchId) {
                        model.isSelected = YES;
                        LSCompetitionModel * cacelModel = [LSCompetitionModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE MatchId=%ld",(long)model.MatchId]];
                        if (cacelModel) {
                            model.pk = cacelModel.pk;
                            model.currentTime = cacelModel.currentTime;
                            [model update];
                        }
                    }
                }];
                LSLayout * layout = [[LSLayout alloc] initWith:model];
                [self.MatchWaitListArray addObject:layout];
            }];
            
            
        }));
        
    });
    DISPATCH_ASYNC_GET_GLOBAL(^{
        DISPATCH_ASYNC_GET_MAIN((^{
            [[response[@"MatchEndList"] mutableCopy] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary * dict = obj;
                LSCompetitionModel * model = [LSCompetitionModel mj_objectWithKeyValues:dict];
                [[LSAccountArchive getSaveEventArray] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString * macId = obj;
                    if (macId.integerValue == model.MatchId) {
                        model.isSelected = YES;
                        LSCompetitionModel * cacelModel = [LSCompetitionModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE MatchId=%ld",(long)model.MatchId]];
                        if (cacelModel) {
                            model.pk = cacelModel.pk;
                            model.currentTime = cacelModel.currentTime;
                            [model update];
                        }
                    }
                }];
                LSLayout * layout = [[LSLayout alloc] initWith:model];
                [self.MatchEndListArray addObject:layout];
            }];
            
            
        }));
        
    });
    DISPATCH_ASYNC_GET_GLOBAL(^{
        DISPATCH_ASYNC_GET_MAIN((^{
            
            [[response[@"MatchDelayList"] mutableCopy] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary * dict = obj;
                LSCompetitionModel * model = [LSCompetitionModel mj_objectWithKeyValues:dict];
                [[LSAccountArchive getSaveEventArray] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString * macId = obj;
                    if (macId.integerValue == model.MatchId) {
                        model.isSelected = YES;
                        LSCompetitionModel * cacelModel = [LSCompetitionModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE MatchId=%ld",model.MatchId]];
                        if (cacelModel) {
                            model.pk = cacelModel.pk;
                            model.currentTime = cacelModel.currentTime;
                            [model update];
                        }
                    }
                }];
                LSLayout * layout = [[LSLayout alloc] initWith:model];
                [self.MatchDelayListArray addObject:layout];
            }];
            
        }));
        
    });
    DISPATCH_ASYNC_GET_GLOBAL(^{
        DISPATCH_ASYNC_GET_MAIN((^{
            [[response[@"MatchCancelList"] mutableCopy] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary * dict = obj;
                LSCompetitionModel * model = [LSCompetitionModel mj_objectWithKeyValues:dict];
                [[LSAccountArchive getSaveEventArray] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString * macId = obj;
                    if (macId.integerValue == model.MatchId) {
                        model.isSelected = YES;
                        LSCompetitionModel * cacelModel = [LSCompetitionModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE MatchId=%ld",(long)model.MatchId]];
                        if (cacelModel) {
                            model.pk = cacelModel.pk;
                            model.currentTime = cacelModel.currentTime;
                            [model update];
                        }
                    }
                }];
                LSLayout * layout = [[LSLayout alloc] initWith:model];
                [self.MatchCancelListArray addObject:layout];
            }];
            
            
        }));
        
    });
    DISPATCH_ASYNC_GET_GLOBAL(^{
        DISPATCH_ASYNC_GET_MAIN((^{
            
            [[response[@"MatchUnsureList"] mutableCopy] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary * dict = obj;
                LSCompetitionModel * model = [LSCompetitionModel mj_objectWithKeyValues:dict];
                [[LSAccountArchive getSaveEventArray] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString * macId = obj;
                    if (macId.integerValue == model.MatchId) {
                        model.isSelected = YES;
                        LSCompetitionModel * cacelModel = [LSCompetitionModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE MatchId=%ld",(long)model.MatchId]];
                        if (cacelModel) {
                            model.pk = cacelModel.pk;
                            model.currentTime = cacelModel.currentTime;
                            [model update];
                        }
                    }
                }];
                LSLayout * layout = [[LSLayout alloc] initWith:model];
                [self.MatchUnsureListArray addObject:layout];
            }];
            
        }));
        
    });
    
    
    
    
    
    
    
    for (NSDictionary * dict in response[@"DataLeagueList"]) {
        LSDataLeagueModel * model = [LSDataLeagueModel mj_objectWithKeyValues:dict];
        if (model.IsTopClassLeague) {
            model.isBtnSelected = YES;
        }else{
            model.isBtnSelected = NO;
        }

        self.zouArray = self.leagueArray.mutableCopy;
        MyLog(@"group end");
        dispatch_async(dispatch_get_main_queue(), ^{
            self.sectionArray = [[NSMutableArray alloc] initWithArray:@[self.MatchRunningListArray,self.MatchWaitListArray,self.MatchEndListArray,self.MatchDelayListArray,self.MatchCancelListArray,self.MatchUnsureListArray]];
            self.modelCopyArray = [[NSMutableArray alloc] initWithArray:@[self.MatchRunningListArray,self.MatchWaitListArray,self.MatchEndListArray,self.MatchDelayListArray,self.MatchCancelListArray,self.MatchUnsureListArray]];
            [self.tableView reloadData];
            [CommonClass endRefreshing:self.tableView];
             self.block(self.leagueArray.mutableCopy,self.leagueArray.mutableCopy,self.modelCopyArray,self.type);
            
            if (self.optionType == 1) {
               [self setLeagueNameArray:self.leagueNameArray];
            }else if (self.optionType == 2){
              [self setLeagueNameArray:self.zouLeagueNamaArray];
            }
        });
        [kLSTimerManeger reload];

        [self.leagueArray addObject:model];
        
    }
    self.zouArray = self.leagueArray.mutableCopy;
    NSLog(@"group end");
    dispatch_async(dispatch_get_main_queue(), ^{
        MyLog(@"endWait = %ld", [self.MatchWaitListArray count]);
        
        self.sectionArray = [[NSMutableArray alloc] initWithArray:@[self.MatchRunningListArray,self.MatchWaitListArray,self.MatchEndListArray,self.MatchDelayListArray,self.MatchCancelListArray,self.MatchUnsureListArray]];
        self.modelCopyArray = [[NSMutableArray alloc] initWithArray:@[self.MatchRunningListArray,self.MatchWaitListArray,self.MatchEndListArray,self.MatchDelayListArray,self.MatchCancelListArray,self.MatchUnsureListArray]];
        [self.tableView reloadData];
        [CommonClass endRefreshing:self.tableView];
        self.block(self.leagueArray.mutableCopy,self.leagueArray.mutableCopy,self.modelCopyArray,self.type);
        if (self.optionType == 1) {
            [self setLeagueNameArray:self.leagueNameArray];
        }else if (self.optionType == 2){
            [self setLeagueNameArray:self.zouLeagueNamaArray];
        }
    });
    [kLSTimerManeger reload];

}
#pragma mark -- 获取关注的数据
- (void)getSaveEvent{
    NSMutableArray * runArray = @[].mutableCopy;
    NSMutableArray * waitArray = @[].mutableCopy;
    NSMutableArray * endArray = @[].mutableCopy;
    NSMutableArray * delayArray = @[].mutableCopy;
    NSMutableArray * cancelArray = @[].mutableCopy;
    NSMutableArray * unsureArray = @[].mutableCopy;
    /**比赛状态(0 未开赛  1 上半场  3 下半场   -1 结束)*/
    for (LSCompetitionModel * model in [LSCompetitionModel findAll]) {
        if (model.MatchState == 0 ) {
            LSLayout * layout = [[LSLayout alloc] initWith:model];
            [waitArray addObject:layout];
        }else if (model.MatchState == 1 || model.MatchState == 3 || model.MatchState == 2 || model.MatchState == 4 || model.MatchState== 5){
            LSLayout * layout = [[LSLayout alloc] initWith:model];
            [runArray addObject:layout];
        }else if (model.MatchState == -1){
            LSLayout * layout = [[LSLayout alloc] initWith:model];
            [endArray addObject:layout];
        }else if (model.MatchState == -14){
            LSLayout * layout = [[LSLayout alloc] initWith:model];
            [delayArray addObject:layout];
        }else if (model.MatchState == -11){
            LSLayout * layout = [[LSLayout alloc] initWith:model];
            [unsureArray addObject:layout];
        }else if (model.MatchState == -10 || model.MatchState == -12 || model.MatchState == -13){
            LSLayout * layout = [[LSLayout alloc] initWith:model];
            [cancelArray addObject:layout];
        }
    }
    self.sectionArray = @[runArray,waitArray,endArray,delayArray,cancelArray].mutableCopy;
    [self.tableView reloadData];
    [CommonClass endRefreshing:self.tableView];
}
#pragma mark -- 筛选数据
- (void)setLeagueNameArray:(NSMutableArray *)leagueNameArray{
    _leagueNameArray = leagueNameArray;
    if (_screeningThePage == 1) {
        self.sectionArray = [[NSMutableArray alloc] initWithArray:self.modelCopyArray copyItems:YES];
        NSMutableArray * delectArray = @[].mutableCopy;
        NSMutableArray * dataArray = @[].mutableCopy;
        for (NSMutableArray * array1 in self.sectionArray) {
            NSMutableArray * array = array1.mutableCopy;
            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                LSLayout * layout = obj;
                LSCompetitionModel * model = layout.model;
                if (leagueNameArray.count != 0) {
                    __block BOOL isHas = YES;
                    [leagueNameArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        NSString * name = obj;
                        if (self.optionType == 1) {
                            if ([name isEqualToString:model.LeagueName_CN]) {
                                isHas = NO;
                            }
                        }else if(self.optionType == 2){
                            if ([name isEqualToString:model.LeagueName_CN] && model.HasZouDi == YES) {
                                isHas = NO;
                            }
                        }
                    }];
                    if (isHas) {
                        [delectArray addObject:layout];
                    }
                }else{
                    if (self.optionType == 1) {
                        [delectArray addObject:layout];
                    }else if (self.optionType == 2){
                        __block BOOL isHas = NO;
                        if (model.HasZouDi) {
                            isHas = YES;
                        }
                        if (isHas) {
                            [delectArray addObject:layout];
                        }
                    }
                    
                }
            }];
            
            [array removeObjectsInArray:delectArray];
            [dataArray addObject:array];
            self.sectionArray = dataArray.mutableCopy;
            [self.tableView reloadData];
            [CommonClass endRefreshing:self.tableView];
            
        }
    }else if(_screeningThePage == 2){
        NSMutableArray * delectArray = @[].mutableCopy;
        if (self.leagueNameArray.count != 0) {
            
            for (LSLayout * layout in self.modelArray) {
                BOOL isHas = YES;
                for (NSString * name  in self.leagueNameArray) {
                    if (self.optionType == 1) {
                        if ([name isEqualToString:layout.model.LeagueName_CN]) {
                            isHas = NO;
                        }
                    }else if(self.optionType == 2){
                        if ([name isEqualToString:layout.model.LeagueName_CN] && layout.model.HasZouDi == YES) {
                            isHas = NO;
                        }
                    }
                }
                if (isHas) {
                    [delectArray addObject:layout];
                }
            }
            [self.modelArray removeObjectsInArray:delectArray];
        }else{
            [self.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                //                LSCompetitionModel * model = obj;
                LSLayout * layout = obj;
                LSCompetitionModel * model = layout.model;
                if (self.optionType == 1) {
                    [delectArray addObject:layout];
                }else if(self.optionType == 2){
                    __block BOOL isHas = NO;
                    
                    if (model.HasZouDi) {
                        isHas = YES;
                        //                    [self.modelArray removeObject:model];
                    }
                    if (isHas) {
                        [delectArray addObject:layout];
                    }
                }
                
            }];
            [self.modelArray removeObjectsInArray:delectArray];
        }
        
        [self.tableView reloadData];
        //        self.modelArray = self.wancCopyArray.mutableCopy;
    }
    //比较2个数组相同的元素
    //    if (self.leagueNameArray.count != 0) {
    //        NSMutableArray * delectArray = @[].mutableCopy;
    //        for (LSCompetitionModel * model in self.modelArray) {
    //            BOOL isHas = YES;
    //            for (NSString * name  in self.leagueNameArray) {
    //                if (self.optionType == 1) {
    //                    if ([name isEqualToString:model.LeagueName_CN]) {
    //                        isHas = NO;
    //                    }
    //                }else if(self.optionType == 2){
    //                    if ([name isEqualToString:model.LeagueName_CN] && [model.ZouDiState integerValue]!= 2) {
    //                        isHas = NO;
    //                    }
    //                }
    //            }
    //            if (isHas) {
    //                [delectArray addObject:model];
    //            }
    //        }
    //        [self.modelArray removeObjectsInArray:delectArray];
    //    }
    //
    //    [self.tableView reloadData];
}

- (void)setZouLeagueNamaArray:(NSMutableArray *)zouLeagueNamaArray{
    
    //    _zouLeagueNamaArray = zouLeagueNamaArray;
    //    if (_screeningThePage == 1) {
    //
    //        self.sectionArray = [[NSMutableArray alloc] initWithArray:self.modelCopyArray copyItems:YES];
    //        NSMutableArray * delectArray = @[].mutableCopy;
    //        NSMutableArray * dataArray = @[].mutableCopy;
    //        for (NSMutableArray * array1 in self.sectionArray) {
    //            NSMutableArray * array = array1.mutableCopy;
    //            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    //                __block BOOL isHas = YES;
    //                LSCompetitionModel * model = obj;
    //                [zouLeagueNamaArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    //                    NSString * name = obj;
    //
    //                    if (self.optionType == 1) {
    //                        if ([name isEqualToString:model.LeagueName_CN]) {
    //                            isHas = NO;
    //                        }
    //                    }else if(self.optionType == 2){
    //                        if ([name isEqualToString:model.LeagueName_CN] && model.HasZouDi == YES) {
    //                            isHas = NO;
    //                        }
    //                    }
    //                }];
    //                if (isHas) {
    //                    [delectArray addObject:model];
    //                }
    //            }];
    //            [array removeObjectsInArray:delectArray];
    //            [dataArray addObject:array];
    //            self.sectionArray = dataArray.mutableCopy;
    //            [self.tableView reloadData];
    //            [CommonClass endRefreshing:self.tableView];
    //
    //        }
    //        //        self.modelArray = self.modelCopyArray.mutableCopy;
    //    }else if(_screeningThePage == 2){
    //        NSMutableArray * delectArray = @[].mutableCopy;
    //        if (self.leagueNameArray.count != 0) {
    //            for (LSCompetitionModel * model in self.modelArray) {
    //                BOOL isHas = YES;
    //                for (NSString * name  in self.leagueNameArray) {
    //                    if (self.optionType == 1) {
    //                        if ([name isEqualToString:model.LeagueName_CN]) {
    //                            isHas = NO;
    //                        }
    //                    }else if(self.optionType == 2){
    //                        if ([name isEqualToString:model.LeagueName_CN] && [model.ZouDiState integerValue]!= 2) {
    //                            isHas = NO;
    //                        }
    //                    }
    //                }
    //                if (isHas) {
    //                    [delectArray addObject:model];
    //                }
    //            }
    //            [self.modelArray removeObjectsInArray:delectArray];
    //        }else{
    //
    //
    //        }
    //
    //        [self.tableView reloadData];
    //    }
}
#pragma mark 🔥 tableViewDelegate tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.type == 1 || self.type == 0) {
        return self.sectionArray.count;
    }
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.type == 0) {
        return [self.sectionArray[section] count];
    }else if (self.type == 1){
        return [self.sectionArray[section] count];
    }else if (self.type == 2){
        return self.modelArray.count;
    }else{
        return self.modelArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type < 2) {
        
        NSMutableArray * array = self.sectionArray[indexPath.section];
        LSLayout * layout = array[indexPath.row];
        return layout.firstH + layout.secondH  + layout.bottomH;
    }
    return 100;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type <3) {
        //        LSCompetitionActionCell *cell=[LSCompetitionActionCell CompetitionActionTableviewCellByTableView:tableView];
        LSCompetitionActionCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
        if (!cell) {
            cell = [[LSCompetitionActionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //根据顶部的类型不同 来传不同的值
        LSLayout * layout;
        if (self.type == 1 || self.type == 0) {
            NSMutableArray * array = self.sectionArray[indexPath.section];
            layout = array[indexPath.row];
        }else{
            layout = self.modelArray[indexPath.row];
        }
        cell.type = self.type;
        cell.layout = layout;
        cell.myIndexPath = indexPath;
        cell.block = ^(UIButton *btn) {
            if (self.type == 0) {
                if (btn.selected == YES) {
                    
                }else{
                    layout.model.isSelected = NO;
                    //                    [model update];
                    [layout.model deleteObject];
                    [LSAccountArchive deletctEventWithEventId:layout.model.MatchId];
                }
                [self getSaveEvent];
            }else{
                NSInteger badgeNumber = 0;
                if (btn.selected == YES) {
                    layout.model.currentTime = [CommonClass getCurrentTimes];
                    layout.model.pk = (int)layout.model.MatchId;
                    layout.model.isSelected = YES;
                    badgeNumber = 1;
                    [LSAccountArchive saveEventWith:layout.model.MatchId];
                    [layout.model save];
                }else{
                    layout.model.isSelected = NO;
                    LSCompetitionModel *model1 = [LSCompetitionModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE MatchId=%ld",(long)layout.model.MatchId]];
                    model1.isSelected = NO;
                    [model1 deleteObject];
                    [LSAccountArchive deletctEventWithEventId:layout.model.MatchId];
                    badgeNumber = -1;
                }
                self.badgeNumberBlock(badgeNumber);
            }
        };
        return cell;
    }else{
        LSMatchScheduleCell * cell = [LSMatchScheduleCell MatchScheduleTableviewCellByTableView:tableView];
        cell.model = self.modelArray[indexPath.row];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    MyLog(@"点击%zd行",indexPath.row);

    if (self.type == 3) {
    }else{
        LSLayout * layout;
        if (self.type == 1 ||self.type == 0) {
            NSMutableArray * array = self.sectionArray[indexPath.section];
            layout = array[indexPath.row];
        }else{
            layout = self.modelArray[indexPath.row];
        }
        LSLiveCompetitionViewController * liveCompetitionVC = [[LSLiveCompetitionViewController alloc] init];
        liveCompetitionVC.matchId = layout.model.MatchId;
        if (layout.model.MatchState == -1 ||layout.model.MatchState == 0) {
            
            liveCompetitionVC.selectIndex = 3;
        }else if(layout.model.MatchState == 1 || layout.model.MatchState == 2 || layout.model.MatchState == 3){
            
            liveCompetitionVC.selectIndex = 2;
        }else{
            
            liveCompetitionVC.selectIndex = 3;
        }
        [self.navigationController pushViewController:liveCompetitionVC animated:YES];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    NSMutableArray * array = self.sectionArray[section];
    if (array.count == 0 && section != 0) {
        return 0.01;
    }
    return 32;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    
    view.tintColor = [UIColor whiteColor];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.type == 3 || self.type == 2) {
        UITableViewHeaderFooterView *_Nullable  headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"fss"];
        if (!headerView) {
            headerView = [[UITableViewHeaderFooterView alloc] init];
            headerView.frame = CGRectMake(0, 0, LSScreenWidth, 32);
            headerView.contentView.backgroundColor = LSTitleWhiteColor;
            UIView * backView = [[UIView alloc] initWithFrame:headerView.frame];
            backView.backgroundColor = [UIColor whiteColor];
            headerView.backgroundView = backView;
            FSSegmentTitleView * titleView = [[FSSegmentTitleView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 32) delegate:self indicatorType:0];
            titleView.backgroundColor = LSTitleWhiteColor;
            titleView.titlesArr = self.timeArray;
            titleView.selectIndex = _scheduleTitleViewSelected;
            titleView.indicatorColor = LSButtonColor;
            [headerView addSubview:titleView];
        }
        return headerView;
    }else{
        NSMutableArray * array = self.sectionArray[section];
        if (array.count == 0 && section != 0) {
            return [UIView new];
        }else{
            if (section == 0) {
                LSCustomHeaderView *_Nullable  headerView =[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CompetitionActionListView"];
                if (!headerView) {
                    headerView=[[LSCustomHeaderView alloc]initWithReuseIdentifier:@"CompetitionActionListView" style:KTableViewHeaderTitleStyle];
                }
                headerView.contentView.backgroundColor=[UIColor whiteColor];
                headerView.timeLab.attributedText = [CommonClass setTitle:self.headerLabelStr color:LSTitleGrayColor2];
                headerView.userInteractionEnabled = YES;
                return headerView;
            }else{
                NSArray * array = @[@"",@"未开始",@"已结束",@"推迟",@"取消",@"待定"];
                LSComSecionHeaderView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"comSetionView"];
                if (!headerView) {
                    headerView = [[LSComSecionHeaderView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 30)];
                }
                headerView.contentView.backgroundColor = LSBackGroundColor;
                headerView.labelTitle = array[section];
                return headerView;
            }
        }
    }
}
#pragma mark -- FSSegmentTitleViewDelegate
- (void)FSSegmentTitleView:(FSSegmentTitleView *)titleView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex{
    _scheduleTitleViewSelected = endIndex;
    _timeStr = [self.scheduleArray[endIndex] componentsSeparatedByString:@" "].firstObject;
    if (self.type == 2) {
        [self loadData:YES withUrlStr:DATABASENDMACTH WithType:self.type with:_timeStr];
    }else{
        [self loadData:YES withUrlStr:DATABASEMATCHSCHEDULE WithType:self.type with:_timeStr];
    }
}
- (void)startTimer{
    NSTimer *timer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(updateData) userInfo:nil repeats:YES];
    // 将定时器添加到runloop中，否则定时器不会启动
    _timer = timer;
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

@end

